import WXAD, { RewardADReturn } from "../../../StartAb/WXAd";
import WXRealLogUtil from "../../../StartAb/WXRealLogUtil";
import EventCenter from "../../TFrame/script/helper/EventCenter";
import SingletonBase from "../../TFrame/script/helper/SingleTonBase";
import EventNames from "../EventNames";
import FireBaseAPI from "../FireBaseAPI";
import NativeCaller from "../NativeCaller";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import PlayerDataMgr from "../PlayerDataMgr";
import ResManager from "../ResManager";
import CoinSystem from "../system/CoinSystem";
import DailyTaskSystem from "../system/DailyTaskSystem";
import ToastHelper from "../ui/ToastHelper";
import StatusProperties, { RewardType } from "./StatusProperties";

export default class GameLogicController extends SingletonBase {

    foundMap = {};

    foundCount = 0;

    hintCount = 0;

    errorCount = 0;

    public reset() {
        this.foundMap = {};
        this.foundCount = 0;
        this.hintCount = 0;
        this.errorCount = 0;
    }

    public findDifference(idx: number, area = 1) {
        if (this.foundMap[idx] && this.foundMap[idx] == 1) {
            return false;
        }
        this.foundMap[idx] = 1;
        this.foundCount += 1;

        EventCenter.emit(EventNames.SpotDiffRight, ResManager.getDiffConfigCoordinate(idx), idx);

        // WXRealLogUtil.info(`第${ResManager.currentLevelIndex+1}关找到第${idx}个`)
        if (this.foundCount >= ResManager.getDiffCount()) {
            EventCenter.emit(EventNames.DiffAllFound);
            DailyTaskSystem.passLevel();
        }
    }

    public isDiffFound(idx: number) {
        if (this.foundMap[idx] && this.foundMap[idx] == 1) {
            return true;
        }
        return false;
    }

    public errorClick(ptWorld: cc.Vec2, area = 1) {
        EventCenter.emit(EventNames.SpotDiffError, ptWorld, area);
        this.errorCount++;
    }

    public hintClick() {
        if (PlayerDataMgr.getInstance().consumeCoinTip() == true) {
            console.log("正常消耗提示数");
            this.successShowHip();
            DailyTaskSystem.useHint();
            EventCenter.emit(EventNames.ConsumeCoins)
            WXRealLogUtil.info(`第${ResManager.currentLevelIndex+1}关金币提示:${CoinSystem.coinInMomory}`)
            // ServiceLogic.action(ActionType.ActionCoinChanged, { "coinhave": CoinSystem.coinInMomory, "beiz": "-" + CoinSystem.TIP_COIN })
            // ServiceLogic.action(ActionType.ActionUseTip, { "coinhave": CoinSystem.coinInMomory, "beiz": "-" + CoinSystem.TIP_COIN })
        } else {
            // ToastHelper.show("金币不足哦,完成任务可领取金币.")
            StatusProperties.rewardType = RewardType.ReqTipInGameScene
            NativeCaller.showReward();
            console.log("请求显示奖励视频来增加提示");
            WXAD.instance.showReward((code: number) => {
                console.log("视频广告回调结果:", code)
                switch(code) {
                    case RewardADReturn.PlayRewarded:
                        GameLogicController.getInstance().successShowHip();
                        //看了视频广告，将插屏显示初始化
                        PlayerDataMgr.getInstance().notShowFullLevelCount = 0;
                        ToastHelper.show("提示成功.")            
                        WXRealLogUtil.info(`第${ResManager.currentLevelIndex+1}关广告提示成功`)

                        break
                    case RewardADReturn.NoAd:
                        WXRealLogUtil.info(`第${ResManager.currentLevelIndex+1}关广告提示失败无广告`)
                        ToastHelper.show("广告未准备好,请稍后重试.")
                        break
                    case RewardADReturn.PlayNotEnd:
                        WXRealLogUtil.info(`第${ResManager.currentLevelIndex+1}关广告提示失败没看完`)
                        ToastHelper.show("要观看完广告才能获得提示哟.")
                        break
                }

            })
            // ServiceLogic.action(ActionType.ActionUseTip, {'beiz': "req ad"})
        }
    }

    public successShowHip() {
        EventCenter.emit(EventNames.ReqShowHint, ResManager.getDiffConfigCoordinate(this.getTipDiffIndex()))
        this.hintCount++;
    }

    private getTipDiffIndex() {
        for (let i = 0; i < ResManager.getDiffCount(); ++i) {
            if (this.foundMap[i] == null)
                return i;
        }
        console.error(`GameLogicController.getTipDiffIndex() error`);
        return 0;
    }

    public calStarCount() {
        if (this.errorCount == 0 && this.hintCount <= 1)
            return 3;
        if (this.errorCount <= 1)
            return 2;
        return 1;
    }

}
